package edu.pku.sei.transformation.editor.controller.editparts.node;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.editpolicies.DirectEditPolicy;

import edu.pku.sei.modeler.edit.parts.AbstractNodeEditPart;
import edu.pku.sei.modeler.model.core.MetaModel;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.transformation.editor.controller.policies.NodeComponentEditPolicy;
import edu.pku.sei.transformation.editor.controller.policies.TransformationNodeEditPolicy;
import edu.pku.sei.transformation.editor.model.ModelPackage;
import edu.pku.sei.transformation.editor.model.ModelType;
import edu.pku.sei.transformation.editor.views.figures.ModelTypeFigure;
import edu.pku.sei.transformation.editor.views.icons.IconsProvider;

public class ModelTypeEditpart extends AbstractNodeEditPart {

	public ModelTypeEditpart(MetaShapeElement model) {
		super(model);
	}

	@Override
	protected DirectEditPolicy createDirectEditPolicy() {
		return null;
	}

	@Override
	protected IFigure createFigure() {
		ModelType metamodel = (ModelType) getNode().getModelElement();
		ModelTypeFigure figure = new ModelTypeFigure();
		figure.setTag("<<Model Type>>");
		figure.setName(metamodel.getName());
		figure
				.setNameLabelIcon(IconsProvider
						.getImage(IconsProvider.MODEL_ICO));
		if (metamodel.getType() != null)
			figure.setContent("MetaModel:"
					+ ((MetaModel) metamodel.getType()).getName());
		else
			figure.setContent("MetaModel:NULL");
		return figure;
	}

	@Override
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE,
				new TransformationNodeEditPolicy());
		installEditPolicy(EditPolicy.COMPONENT_ROLE,
				new NodeComponentEditPolicy());
	}

	@Override
	protected void handlePropertyChanged(PropertyChangeEvent event) {
		ModelTypeFigure figure = (ModelTypeFigure) getFigure();
		ModelType modelType = (ModelType) getNode().getModelElement();
		if (event.getPropertyName().equals(ModelPackage.NAMEDELEMENT_NAME))
			figure.setName(event.getNewValue().toString());
		else if (event.getPropertyName().equals(ModelPackage.TYPEDELEMENT_TYPE)) {
			if (modelType.getType() != null) {
				figure.setContent("MetaModel:"
						+ ((MetaModel) modelType.getType()).getName());
			} else {
				figure.setContent("MetaModel:NULL");
			}
		} else
			super.handlePropertyChanged(event);
	}
}
